package cn.net.dckj.system.service.impl;

import cn.net.dckj.common.core.api.mapper.IBizDao;
import cn.net.dckj.common.core.domain.BaseEntity;
import cn.net.dckj.common.core.impl.GenericBizServiceImpl;
import cn.net.dckj.common.exception.BusinessException;
import cn.net.dckj.common.utils.file.FileUploadUtils;
import cn.net.dckj.common.utils.file.ftp.FTPProperties;
import cn.net.dckj.common.utils.file.ftp.FTPUtils;
import cn.net.dckj.system.domain.SysAttachment;
import cn.net.dckj.system.service.ISysAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 对外服务的抽象接口实现, 封装了基本的对数据库的操作
 * Created by DIY_PC on 2019-5-28.
 */
public abstract class AttachGenericBizServiceImpl<TD extends IBizDao<T, Long>, T extends BaseEntity> extends GenericBizServiceImpl<TD, T> {

    @Autowired
    protected ISysAttachmentService sysAttachmentService;
    @Autowired
    protected FTPProperties ftpProperties;

    /**
     * 新增数据，带多附件上传
     *
     * @param entity
     * @param files
     * @return
     */
    @Override
    @Transactional
    public T insert(T entity, List<MultipartFile> files) {
        /*try {
            T result = super.insert(entity);
            Long id = result.getId();
            if (files != null && files.size() > 0) {
                for (MultipartFile file : files) {
                    if (file != null && !file.isEmpty()) {
                        String fileName = file.getOriginalFilename();
                        long fileSize = file.getSize();
                        String fileType = FileUploadUtils.getExtension(file);
                        String uploadPath = ftpProperties.getUploadUrl();
                        String filePath = FTPUtils.getInstance().uploadFile(uploadPath, file);
                        SysAttachment sysAttachment = new SysAttachment();
                        sysAttachment.setMainId(id);
                        sysAttachment.setEntityName(this.entityClassName);
                        sysAttachment.setAttachmentName(fileName);
                        sysAttachment.setAttachmentSize(fileSize);
                        sysAttachment.setAttachmentType(fileType);
                        sysAttachment.setAttachmentPath(filePath);
                        sysAttachmentService.insert(sysAttachment);
                    }
                }
            }
            return result;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }*/
        return null;
    }

    /********** 以下功能为网站前台和手机端提供服务接口 **********/

}
